import { _decorator, Component, Node } from 'cc';
import { AudioManager } from '../../../common/src/manager/AudioManager';
import { UIManager } from '../../../framework/ui/uimanager/UIManager';
import { GGStore } from '../../../framework/core/base/GGStore';
import { WECHAT } from 'cc/env';
import { MonoUIPanel } from '../../../common/src/mono/MonoUIPanel';
import SDKManager from '../../../common/src/manager/SDKManager';
import DataMgr from '../../../common/src/util/DataMgr';
import Config from '../../../common/src/util/Config';
import { EVENT_NAMES, TIP_ENUMS } from '../../../common/src/util/Enums';
const { ccclass, property } = _decorator;

@ccclass('StaminaDialog')
export class StaminaDialog extends MonoUIPanel {
    // 关卡视频获得的体力
    private videoStaminaCount: number = 3
    
    // =======================================
    // Component 内部方法
    // =======================================
    private addStamina(value: number) {
        if ((DataMgr.stamina + value) > Config.maxStamina) {
            DataMgr.stamina = Config.maxStamina
        } else {
            DataMgr.stamina = DataMgr.stamina + value
        }
        gg.systemEvent.emit(EVENT_NAMES.UPDATE_START_INFO)
    }

    // =======================================
    // Component 事件
    // =======================================
    /** 关闭面板. */
    private async onClosePanel() {
        AudioManager.playButton()
        await this.onExitAnimation()
        UIManager.close("stamina:res/prefab/StaminaDialog")
    }

    // 使用视频来补充体力
    public onAddPower(){
        AudioManager.playButton()
        const self = this
        if (WECHAT) {
            SDKManager.showRewardVideoAd("补充体力", (err, res) => {
                if (!err) {
                    this.addStamina(self.videoStaminaCount)
                    UIManager.close("stamina:res/prefab/StaminaDialog")
                } else {
                    UIManager.open("tip:res/prefab/TipToast", (err, node:Node) => {
                        if(res.code == 1001){ 
                            node.getComponent("TipToast").setData(TIP_ENUMS.LOAD_VIDEO_FAIL)
                        }else{
                            node.getComponent("TipToast").setData(TIP_ENUMS.NOT_FULL_VIDEO)
                        }
                    });  
                }
            })
        } else {
            this.addStamina(self.videoStaminaCount)
            UIManager.close("stamina:res/prefab/StaminaDialog")
        }
    }

    // 支付金币
    public onMoneyAddPower(){
        AudioManager.playButton();
        if (DataMgr.money >= Config.videoAddMoney) {
            DataMgr.money = DataMgr.money - 100
            this.addStamina(1)
            UIManager.close("stamina:res/prefab/StaminaDialog")
        } else {
            if (WECHAT) {
                SDKManager.showRewardVideoAd("金币不足再补充", (err, res) => {
                    if (!err) {
                        this.addStamina(1)
                        UIManager.close("stamina:res/prefab/StaminaDialog")
                    } else {
                        UIManager.open("tip:res/prefab/TipToast", (err, node:Node) => {
                            if(res.code == 1001){ 
                                node.getComponent("TipToast").setData(TIP_ENUMS.LOAD_VIDEO_FAIL)
                            }else{
                                node.getComponent("TipToast").setData(TIP_ENUMS.NOT_FULL_VIDEO)
                            }
                        });  
                    }
                })
            } else {
                this.addStamina(1)
                UIManager.close("stamina:res/prefab/StaminaDialog")
            }
        }
    }
}


